package com.koron.order.experiment.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.experiment.bean.po.ExperimentReadyPo;
import com.koron.order.experiment.mapper.ExperimentReadyMapper;
import com.koron.order.experiment.service.ExperimentReadyService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName ExperimentReadyServiceIMpl
 * @description
 * @Author zhouj
 * @Date 2021/10/9 14:10
 */
@Service("experimentReadyService")
public class ExperimentReadyServiceImpl implements ExperimentReadyService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, ExperimentReadyPo experimentReady) {

        ExperimentReadyMapper mapper = factory.getMapper(ExperimentReadyMapper.class);
        experimentReady.setId(CodeTools.getCode32());

        return mapper.insert(experimentReady);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, ExperimentReadyPo experimentReady) {
        ExperimentReadyMapper mapper = factory.getMapper(ExperimentReadyMapper.class);
        return mapper.update(experimentReady);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        ExperimentReadyMapper mapper = factory.getMapper(ExperimentReadyMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {

        ExperimentReadyMapper mapper = factory.getMapper(ExperimentReadyMapper.class);
        return mapper.deleteByOrderId(orderId);
    }

    @Override
    @TaskAnnotation("queryById")
    public ExperimentReadyPo queryById(SessionFactory factory, String id) {

        ExperimentReadyMapper mapper = factory.getMapper(ExperimentReadyMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<ExperimentReadyPo> queryList(SessionFactory factory, ExperimentReadyPo experimentReady) {

        ExperimentReadyMapper mapper = factory.getMapper(ExperimentReadyMapper.class);

        List<ExperimentReadyPo> list = mapper.queryList(experimentReady);

        return list;
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public List<ExperimentReadyPo> queryByOrderId(SessionFactory factory, String orderId) {

        ExperimentReadyMapper mapper = factory.getMapper(ExperimentReadyMapper.class);

        List<ExperimentReadyPo> list = mapper.queryByOrderId(orderId);

        return list;
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<ExperimentReadyPo> experimentReadyPoList) {

        if (CollectionUtils.isEmpty(experimentReadyPoList)) {
            return 0;
        }
        String experimentId = experimentReadyPoList.get(0).getExperimentId();
        deleteByOrderId(factory, experimentId);

        ExperimentReadyMapper mapper = factory.getMapper(ExperimentReadyMapper.class);
        experimentReadyPoList.forEach(p -> {
            p.setId(CodeTools.getCode32());
        });

        return mapper.insertBatch(experimentReadyPoList);
    }

    @Override
    @TaskAnnotation("updateBatch")
    public int updateBatch(SessionFactory factory, List<ExperimentReadyPo> experimentReadyPoList) {

        if (CollectionUtils.isEmpty(experimentReadyPoList)) {
            return 0;
        }

        ExperimentReadyMapper mapper = factory.getMapper(ExperimentReadyMapper.class);

        return mapper.updateBatch(experimentReadyPoList);
    }

}
